/**
 ******************************************************************************
 * @file           : bsp_usart_config.h
 * @brief          : USART Configuration File (STM32F1xx)
 ******************************************************************************
 * @attention
 *
 * USART硬件配置文件
 * 包含引脚定义、波特率配置等硬件相关参数
 * 用户根据实际硬件修改此文件
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BSP_USART_CONFIG_H
#define __BSP_USART_CONFIG_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

/* ============================================================================ */
/*                       日志系统配置（改这里的数字）                             */
/* ============================================================================ */
/* 日志后端选择：1=EasyLogger, 2=printf, 0=关闭 */
#define USART_USE_LOG     1

/* 根据配置自动定义日志宏 */
#if (USART_USE_LOG == 1)
    /* 使用EasyLogger */
    #ifndef LOG_TAG
        #define LOG_TAG    "USART"
    #endif
    #include "elog.h"
    
#elif (USART_USE_LOG == 2)
    /* 使用printf */
    #include <stdio.h>
    #define log_i(fmt, ...)     printf(fmt "\r\n", ##__VA_ARGS__)
    #define log_w(fmt, ...)     printf("[WARN] " fmt "\r\n", ##__VA_ARGS__)
    #define log_e(fmt, ...)     printf("[ERROR] " fmt "\r\n", ##__VA_ARGS__)
    #define log_d(fmt, ...)     printf("[DEBUG] " fmt "\r\n", ##__VA_ARGS__)
    
#else
    /* 关闭日志 */
    #define log_i(fmt, ...)
    #define log_w(fmt, ...)
    #define log_e(fmt, ...)
    #define log_d(fmt, ...)
#endif

/* ============================================================================ */
/*                      串口选择配置（改这里即可切换串口）                         */
/* ============================================================================ */
/**
 * @brief 串口选择（1=USART1, 2=USART2, 3=USART3, 4=UART4, 5=UART5）
 * 
 * 修改此值即可切换串口，引脚和外设配置会自动更改
 * 支持的串口及默认引脚：
 *   USART1: TX=PA9,  RX=PA10  (支持重映射到 PB6/PB7)
 *   USART2: TX=PA2,  RX=PA3   (支持重映射到 PD5/PD6)
 *   USART3: TX=PB10, RX=PB11  (支持重映射到 PC10/PC11 或 PD8/PD9)
 *   UART4:  TX=PC10, RX=PC11
 *   UART5:  TX=PC12, RX=PD2
 */
#define USART_SELECT_NUM                4       /*!< 选择使用的串口号 */

/**
 * @brief 引脚重映射配置（仅USART1/2/3支持）
 * 
 * 0 = 不重映射（使用默认引脚）
 * 1 = 部分重映射（USART1/3）或完全重映射（USART2）
 * 2 = 完全重映射（仅USART3）
 */
#define USART_REMAP_MODE                0       /*!< 引脚重映射模式 */

/* ============================================================================ */
/*                    自动配置区域（请勿手动修改）                                */
/* ============================================================================ */

/* --------------------------------------------------------------------------- */
/* USART1 配置 (PA9/PA10 或 PB6/PB7)                                            */
/* --------------------------------------------------------------------------- */
#if (USART_SELECT_NUM == 1)
    #define USART_UARTx                     USART1
    #define USART_UARTx_IRQn                USART1_IRQn
    #define USART_UARTx_CLK_ENABLE()        do{ __HAL_RCC_USART1_CLK_ENABLE(); }while(0)
    #define USARTx_IRQHandler               USART1_IRQHandler
    
    #if (USART_REMAP_MODE == 1)  /* 重映射到PB6/PB7 */
        #define USART_TX_GPIO_PORT              GPIOB
        #define USART_TX_GPIO_PIN               GPIO_PIN_6
        #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0)
        
        #define USART_RX_GPIO_PORT              GPIOB
        #define USART_RX_GPIO_PIN               GPIO_PIN_7
        #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0)
        
        #define USART_GPIO_REMAP_CONFIG()       do{ __HAL_AFIO_REMAP_USART1_ENABLE(); }while(0)
    #else  /* 默认PA9/PA10 */
        #define USART_TX_GPIO_PORT              GPIOA
        #define USART_TX_GPIO_PIN               GPIO_PIN_9
        #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)
        
        #define USART_RX_GPIO_PORT              GPIOA
        #define USART_RX_GPIO_PIN               GPIO_PIN_10
        #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)
        
        #define USART_GPIO_REMAP_CONFIG()       /* 不需要重映射 */
    #endif

/* --------------------------------------------------------------------------- */
/* USART2 配置 (PA2/PA3 或 PD5/PD6)                                             */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 2)
    #define USART_UARTx                     USART2
    #define USART_UARTx_IRQn                USART2_IRQn
    #define USART_UARTx_CLK_ENABLE()        do{ __HAL_RCC_USART2_CLK_ENABLE(); }while(0)
    #define USARTx_IRQHandler               USART2_IRQHandler
    
    #if (USART_REMAP_MODE == 1)  /* 重映射到PD5/PD6 */
        #define USART_TX_GPIO_PORT              GPIOD
        #define USART_TX_GPIO_PIN               GPIO_PIN_5
        #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOD_CLK_ENABLE(); }while(0)
        
        #define USART_RX_GPIO_PORT              GPIOD
        #define USART_RX_GPIO_PIN               GPIO_PIN_6
        #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOD_CLK_ENABLE(); }while(0)
        
        #define USART_GPIO_REMAP_CONFIG()       do{ __HAL_AFIO_REMAP_USART2_ENABLE(); }while(0)
    #else  /* 默认PA2/PA3 */
        #define USART_TX_GPIO_PORT              GPIOA
        #define USART_TX_GPIO_PIN               GPIO_PIN_2
        #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)
        
        #define USART_RX_GPIO_PORT              GPIOA
        #define USART_RX_GPIO_PIN               GPIO_PIN_3
        #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)
        
        #define USART_GPIO_REMAP_CONFIG()       /* 不需要重映射 */
    #endif

/* --------------------------------------------------------------------------- */
/* USART3 配置 (PB10/PB11 或 PC10/PC11 或 PD8/PD9)                              */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 3)
    #define USART_UARTx                     USART3
    #define USART_UARTx_IRQn                USART3_IRQn
    #define USART_UARTx_CLK_ENABLE()        do{ __HAL_RCC_USART3_CLK_ENABLE(); }while(0)
    #define USARTx_IRQHandler               USART3_IRQHandler
    
    #if (USART_REMAP_MODE == 1)  /* 部分重映射到PC10/PC11 */
        #define USART_TX_GPIO_PORT              GPIOC
        #define USART_TX_GPIO_PIN               GPIO_PIN_10
        #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOC_CLK_ENABLE(); }while(0)
        
        #define USART_RX_GPIO_PORT              GPIOC
        #define USART_RX_GPIO_PIN               GPIO_PIN_11
        #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOC_CLK_ENABLE(); }while(0)
        
        #define USART_GPIO_REMAP_CONFIG()       do{ __HAL_AFIO_REMAP_USART3_PARTIAL(); }while(0)
    #elif (USART_REMAP_MODE == 2)  /* 完全重映射到PD8/PD9 */
        #define USART_TX_GPIO_PORT              GPIOD
        #define USART_TX_GPIO_PIN               GPIO_PIN_8
        #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOD_CLK_ENABLE(); }while(0)
        
        #define USART_RX_GPIO_PORT              GPIOD
        #define USART_RX_GPIO_PIN               GPIO_PIN_9
        #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOD_CLK_ENABLE(); }while(0)
        
        #define USART_GPIO_REMAP_CONFIG()       do{ __HAL_AFIO_REMAP_USART3_ENABLE(); }while(0)
    #else  /* 默认PB10/PB11 */
        #define USART_TX_GPIO_PORT              GPIOB
        #define USART_TX_GPIO_PIN               GPIO_PIN_10
        #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0)
        
        #define USART_RX_GPIO_PORT              GPIOB
        #define USART_RX_GPIO_PIN               GPIO_PIN_11
        #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0)
        
        #define USART_GPIO_REMAP_CONFIG()       /* 不需要重映射 */
    #endif

/* --------------------------------------------------------------------------- */
/* UART4 配置 (PC10/PC11)                                                       */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 4)
    #define USART_UARTx                     UART4
    #define USART_UARTx_IRQn                UART4_IRQn
    #define USART_UARTx_CLK_ENABLE()        do{ __HAL_RCC_UART4_CLK_ENABLE(); }while(0)
    #define USARTx_IRQHandler               UART4_IRQHandler
    
    #define USART_TX_GPIO_PORT              GPIOC
    #define USART_TX_GPIO_PIN               GPIO_PIN_10
    #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOC_CLK_ENABLE(); }while(0)
    
    #define USART_RX_GPIO_PORT              GPIOC
    #define USART_RX_GPIO_PIN               GPIO_PIN_11
    #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOC_CLK_ENABLE(); }while(0)
    
    #define USART_GPIO_REMAP_CONFIG()       /* UART4不支持重映射 */

/* --------------------------------------------------------------------------- */
/* UART5 配置 (PC12/PD2)                                                        */
/* --------------------------------------------------------------------------- */
#elif (USART_SELECT_NUM == 5)
    #define USART_UARTx                     UART5
    #define USART_UARTx_IRQn                UART5_IRQn
    #define USART_UARTx_CLK_ENABLE()        do{ __HAL_RCC_UART5_CLK_ENABLE(); }while(0)
    #define USARTx_IRQHandler               UART5_IRQHandler
    
    #define USART_TX_GPIO_PORT              GPIOC
    #define USART_TX_GPIO_PIN               GPIO_PIN_12
    #define USART_TX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOC_CLK_ENABLE(); }while(0)
    
    #define USART_RX_GPIO_PORT              GPIOD
    #define USART_RX_GPIO_PIN               GPIO_PIN_2
    #define USART_RX_GPIO_CLK_ENABLE()      do{ __HAL_RCC_GPIOD_CLK_ENABLE(); }while(0)
    
    #define USART_GPIO_REMAP_CONFIG()       /* UART5不支持重映射 */

#else
    #error "USART_SELECT_NUM must be 1~5!"
#endif

/* ============================================================================ */
/*                          功能配置（用户可修改）                               */
/* ============================================================================ */

/* USART缓冲区大小 */
#define USART_RX_BUF_SIZE               256     /*!< 接收缓冲区大小(字节) */
#define USART_TX_BUF_SIZE               256     /*!< 发送缓冲区大小(字节) */

/* USART超时配置 */
#define USART_TX_TIMEOUT                1000    /*!< 发送超时时间(ms) */
#define USART_RX_TIMEOUT                100     /*!< 接收超时时间(ms) */

/* USART默认波特率 */
#define USART_DEFAULT_BAUDRATE          115200  /*!< 默认波特率 */

/* printf重定向配置 */
#define USART_PRINTF_ENABLE             1       /*!< 使能printf重定向(1:使能, 0:禁用) */

/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

#ifdef __cplusplus
}
#endif

#endif /* __BSP_USART_CONFIG_H */
